package com.jytc.framework.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * 
 * @title: RequestUtils.java
 * @description: Request 工具类
 * @Copyright: Copyright (c) 2009-8-10
 * @Company: 南京欣网视讯软件技术有限公司
 * @description: Yao
 * 
 */
public final class RequestUtils
{
	/**
	 * 将请求信息根据名称匹配绑定在一个bean上
	 * 
	 * @param bean
	 *            所要绑定的Bean的引用
	 * 
	 * @param request
	 *            request请求
	 * @throws ServletException
	 */
	@SuppressWarnings("unchecked")
	public static void requestBind(Object bean, HttpServletRequest request) throws ServletException
	{
		Map parameterMap = new HashMap();
		String contentType = request.getContentType();
		String method = request.getMethod();
		boolean isMultipart = false;

		if ((contentType != null) && (contentType.startsWith("multipart/form-data")) && (method.equalsIgnoreCase("POST")))
		{
			isMultipart = true;
			MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
			Map fileMap = multipartHttpServletRequest.getFileMap();
			parameterMap = multipartHttpServletRequest.getParameterMap();
			parameterMap.putAll(fileMap);
		}

		if (!isMultipart)
		{
			parameterMap = request.getParameterMap();
		}
		try
		{
			BeanUtils.populate(bean, parameterMap);
		} catch (Exception e)
		{
			throw new ServletException("BeanUtils.populate", e);
		}

	}

	public static String getParameter(HttpServletRequest request, String name)
	{
		return getParameter(request, name, false);
	}

	public static String getParameter(HttpServletRequest request, String name, boolean emptyStringsOK)
	{
		String temp = request.getParameter(name);
		if (temp != null)
		{
			if ("".equals(temp) && !emptyStringsOK)
				return null;
			else
				return temp;
		} else
		{
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public static String[] getParameters(HttpServletRequest request, String name)
	{
		if (name == null)
			return new String[0];
		String[] paramValues = request.getParameterValues(name);
		if (paramValues == null || paramValues.length == 0)
			return new String[0];
		List values = new ArrayList(paramValues.length);
		for (int i = 0; i < paramValues.length; i++)
			if (paramValues[i] != null && !"".equals(paramValues[i]))
				values.add(paramValues[i]);

		return (String[]) values.toArray(new String[0]);
	}

	public static boolean getBooleanParameter(HttpServletRequest request, String name)
	{
		return getBooleanParameter(request, name, false);
	}

	public static boolean getBooleanParameter(HttpServletRequest request, String name, boolean defaultVal)
	{
		String temp = request.getParameter(name);
		if ("true".equals(temp) || "on".equals(temp))
			return true;
		if ("false".equals(temp) || "off".equals(temp))
			return false;
		else
			return defaultVal;
	}

	public static int getIntParameter(HttpServletRequest request, String name, int defaultNum)
	{
		String temp = request.getParameter(name);
		if (temp != null && !"".equals(temp))
		{
			int num = defaultNum;
			try
			{
				num = Integer.parseInt(temp);
			} catch (Exception exception)
			{
			}
			return num;
		} else
		{
			return defaultNum;
		}
	}

	public static int[] getIntParameters(HttpServletRequest request, String name, int defaultNum)
	{
		String[] paramValues = request.getParameterValues(name);
		if (paramValues == null || paramValues.length == 0)
			return new int[0];
		int[] values = new int[paramValues.length];
		for (int i = 0; i < paramValues.length; i++)
			try
			{
				values[i] = Integer.parseInt(paramValues[i]);
			} catch (Exception e)
			{
				values[i] = defaultNum;
			}

		return values;
	}

	public static double getDoubleParameter(HttpServletRequest request, String name, double defaultNum)
	{
		String temp = request.getParameter(name);
		if (temp != null && !"".equals(temp))
		{
			double num = defaultNum;
			try
			{
				num = Double.parseDouble(temp);
			} catch (Exception exception)
			{
			}
			return num;
		} else
		{
			return defaultNum;
		}
	}

	public static long getLongParameter(HttpServletRequest request, String name, long defaultNum)
	{
		String temp = request.getParameter(name);
		if (temp != null && !"".equals(temp))
		{
			long num = defaultNum;
			try
			{
				num = Long.parseLong(temp);
			} catch (Exception exception)
			{
			}
			return num;
		} else
		{
			return defaultNum;
		}
	}

	public static long[] getLongParameters(HttpServletRequest request, String name, long defaultNum)
	{
		String[] paramValues = request.getParameterValues(name);
		if (paramValues == null || paramValues.length == 0)
			return new long[0];
		long[] values = new long[paramValues.length];
		for (int i = 0; i < paramValues.length; i++)
			try
			{
				values[i] = Long.parseLong(paramValues[i]);
			} catch (Exception e)
			{
				values[i] = defaultNum;
			}

		return values;
	}

	public static String getAttribute(HttpServletRequest request, String name)
	{
		return getAttribute(request, name, false);
	}

	public static String getAttribute(HttpServletRequest request, String name, boolean emptyStringsOK)
	{
		String temp = (String) request.getAttribute(name);
		if (temp != null)
		{
			if ("".equals(temp) && !emptyStringsOK)
				return null;
			else
				return temp;
		} else
		{
			return null;
		}
	}

	public static boolean getBooleanAttribute(HttpServletRequest request, String name)
	{
		String temp = (String) request.getAttribute(name);
		return temp != null && "true".equals(temp);
	}

	public static int getIntAttribute(HttpServletRequest request, String name, int defaultNum)
	{
		String temp = (String) request.getAttribute(name);
		if (temp != null && !"".equals(temp))
		{
			int num = defaultNum;
			try
			{
				num = Integer.parseInt(temp);
			} catch (Exception exception)
			{
			}
			return num;
		} else
		{
			return defaultNum;
		}
	}

	public static long getLongAttribute(HttpServletRequest request, String name, long defaultNum)
	{
		String temp = (String) request.getAttribute(name);
		if (temp != null && !"".equals(temp))
		{
			long num = defaultNum;
			try
			{
				num = Long.parseLong(temp);
			} catch (Exception exception)
			{
			}
			return num;
		} else
		{
			return defaultNum;
		}
	}
}
